/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gov.nist.ssd.swqb.core.files;

import gov.nist.ssd.swqb.core.ConfigFileManager;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Benjamin Herbomez
 */
public class PairFileManager  extends FileManager{

    protected Map<String,String> pairs = new LinkedHashMap<String, String>();
    
    public PairFileManager(ConfigFileManager fm){
        super(fm);
    }
    
    @Override
    public void save() {
        String txt = "";
        for (String k : this.pairs.keySet()) {
            txt += k + "\t" + this.pairs.get(k) + "\n";
        }
        this.saveIntern(txt);
    }

    protected void init() {
        String fileDir = fm.getFileLocation(this.fileName, true);
        if (fileDir != null) {
            //Create the file
            this.fileLocation = fileDir;

            FileInputStream fsteam = null;
            try {
                fsteam = new FileInputStream(this.fileLocation);
                DataInputStream in = new DataInputStream(fsteam);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String line = "";
                while ((line = br.readLine()) != null) {
                    /* Read data
                     * Format : namespace tabulation prefix
                     */
                    String[] split = line.split("\t");
                    if (split.length == 2) {
                        this.pairs.put(split[0], split[1]);
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(PrefixManager.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    fsteam.close();
                } catch (IOException ex) {
                    Logger.getLogger(PrefixManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
    }
    
}
